<?php

class Admin_ParcelamentoController extends App_Controller_Action
{

    private $_table     = 'PARCELAMENTO';
    private $_pk        = 'PAR_NUM';
    private $_pk2       = 'MAT_ID';
    private $_order_col = 'PAR_NUM';
    private $_order     = 'ASC';
    private $_sta_col   = 'PAR_STA';
    private $obj        = '';
    private $form       = '';
    
    public function init()
    {
        $this->obj = new Admin_Model_DbTable_Parcelamento();
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Admin_Form_Parcelamento();
        
        $this->view->voltar =  $this->_urlbase . '/' . $this->_modulo . '/matricula/';
        
    }

    public function indexAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('dominio','library');
        Zend_Loader::loadClass('operacoes','library');
        Zend_Loader::loadClass('elemform','library');
        
        $id2 = $this->_getParam('mat_id',0);
        $this->view->MAT_ID = $id2;
        
        $where = 'p.'.$this->_pk2 . ' = ' . $id2;
       
        //$filtro = $this->_getParam('itfiltro', '1'); 
        //if($filtro != '') $where .= ' and PAR_STA = ' . $filtro;
        //$this->view->filtro = $filtro;
        
        //$result = $this->obj->fetchAll($this->obj->select()->where($where)->order($this->_order_col.' '.$this->_order));
        //$this->view->result = $this->paginacao($result,12);
        
        //$mat = new Admin_Model_DbTable_Matricula();
        //$this->view->mat = $mat->fetchRow('MAT_ID = ' . $id2);
        
        $query = 'SELECT SQL_CACHE p.*,m.CUR_ID, f.FOR_ID, f.FOR_NOM, a.ALU_NOM '
                 .'FROM '.$this->_table.' p, MATRICULA m, FORMAS_PAGAMENTO f, ALUNO a '
                 .'WHERE p.MAT_ID = m.MAT_ID AND p.FOR_ID = f.FOR_ID AND m.ALU_ID = a.ALU_ID AND '
                 .$where.' ORDER BY p.'.$this->_order_col.' '.$this->_order;
        
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result,12);
        
    }
    
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        
        $this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if ($this->form->isValid($formData)) {
                    $id = (int) $this->form->getValue($this->_pk);
                    $id2 = (int) $this->form->getValue($this->_pk2);
                    
                    $data = $this->form->getValues();
                    $data['PAR_DAT'] = Datetools::dma_amd($data['PAR_DAT']);
                    unset($data['CUR_NOM']);
                    unset($data['FOR_NOM']);
                    unset($data['ALU_NOM']);
                    
                    if ($id == 0){
                        $max = $this->obj->fetchRow($this->obj->select()->from($this->obj, array(new Zend_Db_Expr('max(PAR_NUM) as MAX')))->where($this->_pk2 . ' = ' . $id2))->toArray();
                        $data['PAR_NUM'] = $max['MAX'] + 1;
                        $data['PAR_STA'] = 1;
                        $this->obj->insert($data);
                    }
                    else $this->obj->update($data, $this->_pk.' = ' . $id . ' and ' . $this->_pk2 . ' = ' . $id2);
                    
                    $vars = array('mat_id'=>$data['MAT_ID']);
                    $this->_helper->redirector('index', $this->_controller,$this->_module, $vars);
                    
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                // echo $ex->getMessage();
                $this->form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            $id2 = $this->_getParam('mat_id',0);
            if ($id2 > 0) {
                if($id > 0) {
                    $data = $this->obj->fetchRow($this->_pk . ' = ' . $id . ' and ' . $this->_pk2 . ' = ' . $id2)->toArray();
                    $data['PAR_DAT'] = Datetools::amd_dma($data['PAR_DAT']);
                    $data['PAR_VLR'] = tools::float_to_money($data['PAR_VLR']);
                } else{
                    $data['MAT_ID'] = $id2;
                }
                $mat = new Admin_Model_DbTable_Matricula();
                $r_mat = $mat->fetchRow('MAT_ID = ' . $data['MAT_ID'])->toArray();
                
                $cur = new Admin_Model_DbTable_Curso();
                $r_cur = $cur->fetchRow('CUR_ID = ' . $r_mat['CUR_ID'])->toArray();
                $data['CUR_NOM'] = $r_cur['CUR_NOM'];
                
                $for = new Admin_Model_DbTable_Formaspagamento();
                $r_for = $for->fetchRow('FOR_ID = ' . $r_mat['FOR_ID'])->toArray();
                $data['FOR_NOM'] = $r_for['FOR_NOM'];
                
                $alu = new Admin_Model_DbTable_Aluno();
                $r_alu = $alu->fetchRow('ALU_ID = ' . $r_mat['ALU_ID'])->toArray();
                $data['ALU_NOM'] = $r_alu['ALU_NOM'];
                
                $this->form->populate($data);
                
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        $id2 = $this->_getParam('mat_id',0);
        if ((int)$id > 0 ) {   
            $data = array($this->_sta_col => 0);
            $ok = (bool) $this->obj->update($data, $this->_pk . ' = ' . $id . ' and ' . $this->_pk2 . ' = ' . $id2);
        }       
        
        $vars = array('mat_id'=>$id2);
        $this->_helper->redirector('index', $this->_controller,$this->_module, $vars);
        //echo json_encode(array('ok' => $ok));
    }
    
    public function boletoAction() {
        //$this->_helper->layout->disableLayout(); 
        $this->_helper->layout->setLayout("layout_clean");
        
        Zend_Loader::loadClass('datetools','library');
        
        $id = $this->_getParam('id', 0);
        $id2 = $this->_getParam('mat_id', 0);
        if($id > 0 || $id2 > 0) {

            $query = 'SELECT SQL_CACHE * FROM PARCELAMENTO p, MATRICULA m, ALUNO a ';
            $query.= ' WHERE p.MAT_ID = m.MAT_ID AND p.PAR_STAPGO = 0 AND m.ALU_ID = a.ALU_ID ';
            if($id > 0) $query.= ' AND p.PAR_NUM = ' . $id;
            if($id2 > 0) $query.= ' AND p.MAT_ID = ' . $id2;
            
            $parc = $this->db->fetchAll($query);
            $this->view->parc = $parc;                     

        }else{
            
            $vars = array('mat_id'=>$id2);
            $this->_helper->redirector('index', $this->_controller,$this->_module, $vars);
            
        }
            
        /*
        $query = 'SELECT SQL_CACHE * FROM BOLETO_BANCARIO WHERE PAR_NUM = ' . $id . ' and MAT_ID = ' . $id2;
        $bol = $this->db->fetchRow($query);
        if($bol['BLB_ID'] <= 0){

            $arr_inser = array();

            $arr_inser['BLB_ININOSNUM'] = $dadosboleto["inicio_nosso_numero"];
            $arr_inser['BLB_NOSNUM'] = $dadosboleto["nosso_numero"];
            $arr_inser['BLB_NUMDOC'] = $dadosboleto["numero_documento"];
            $arr_inser['BLB_DATVEN'] = Datetools::dma_amd($dadosboleto["data_vencimento"]);
            $arr_inser['BLB_DATDOC'] = Datetools::dma_amd($dadosboleto["data_documento"]);
            $arr_inser['BLB_DATPRO'] = Datetools::dma_amd($dadosboleto["data_processamento"]); 
            $arr_inser['BLB_VALBOL'] = $dadosboleto["valor_boleto"];
            $arr_inser['BLB_VALUNI'] = $dadosboleto["valor_unitario"];
            $arr_inser['BLB_QTD'] = $dadosboleto["quantidade"];                 
            $arr_inser['BLB_STA'] = ''; 
            $arr_inser['PAR_NUM'] = $id;
            $arr_inser['MAT_ID'] = $id2;

            $this->db->insert('BOLETO_BANCARIO', $arr_inser);

            $query = 'SELECT SQL_CACHE * FROM BOLETO_BANCARIO WHERE PAR_NUM = ' . $id . ' and MAT_ID = ' . $id2;
            $bol = $this->db->fetchRow($query);
        }
        */
                
    }

}